home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / urlsnarf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-19  |  6.2 KB  |  259 lines

  1. /*
  2.   urlsnarf.c
  3.   
  4.   sniff the network for HTTP request URLs, output in CLF format.
  5.   this is for demonstration purposes and educational use only.
  6.  
  7.   Copyright (c) 1999 Dug Song <dugsong@monkey.org>
  8.   All rights reserved, all wrongs reversed.
  9.  
  10.   Redistribution and use in source and binary forms, with or without
  11.   modification, are permitted provided that the following conditions
  12.   are met:
  13.  
  14.   1. Redistributions of source code must retain the above copyright
  15.      notice, this list of conditions and the following disclaimer.
  16.   2. Redistributions in binary form must reproduce the above copyright
  17.      notice, this list of conditions and the following disclaimer in the
  18.      documentation and/or other materials provided with the distribution.
  19.   3. The name of author may not be used to endorse or promote products
  20.      derived from this software without specific prior written permission.
  21.  
  22.   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  23.   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24.   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  25.   THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26.   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27.   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28.   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29.   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30.   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.  
  33.   $Id: urlsnarf.c,v 1.23 2000/05/19 05:05:55 dugsong Exp $
  34. */
  35.  
  36. #include <sys/types.h>
  37. #include <sys/socket.h>
  38. #include <netinet/in.h>
  39. #include <arpa/inet.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <string.h>
  44. #include <time.h>
  45. #include <libnet.h>
  46. #include <nids.h>
  47. #include "base64.h"
  48.  
  49. #include "version.h"
  50.  
  51. /* Globals. */
  52. u_short Opt_dns = 1;
  53.  
  54. void
  55. usage(void)
  56. {
  57.   fprintf(stderr, "Usage: urlsnarf [-n] [-i interface]\n");
  58.   exit(1);
  59. }
  60.  
  61. /* Locate substring in a binary string. */
  62. u_char *
  63. bufbuf(u_char *big, int blen, u_char *little, int llen)
  64. {
  65.   u_char *p;
  66.  
  67.   for (p = big; p <= big + blen - llen; p++) {
  68.     if (memcmp(p, little, llen) == 0)
  69.       return (p);
  70.   }
  71.   return (NULL);
  72. }
  73.   
  74. /* XXX - blech. */
  75. char *
  76. get_request_time(void)
  77. {
  78.   static char tstr[32], sign;
  79.   struct tm *t, gmt;
  80.   time_t tt = time(NULL);
  81.   int days, hours, tz, len;
  82.  
  83.   gmt = *gmtime(&tt);
  84.   t = localtime(&tt);
  85.   
  86.   days = t->tm_yday - gmt.tm_yday;
  87.   hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) +
  88.        t->tm_hour - gmt.tm_hour);
  89.   tz = hours * 60 + t->tm_min - gmt.tm_min;
  90.  
  91.   len = strftime(tstr, sizeof(tstr), "%e/%b/%Y:%X", t);
  92.   if (len < 0 || len > sizeof(tstr) - 5)
  93.     return (NULL);
  94.  
  95.   if (tz < 0) {
  96.     sign = '-';
  97.     tz = -tz;
  98.   }
  99.   else sign = '+';
  100.   
  101.   snprintf(tstr + len, sizeof(tstr) - len, " %c%.2d%.2d",
  102.        sign, tz / 60, tz % 60);
  103.   
  104.   return (tstr);
  105. }
  106.  
  107. int
  108. http_method_len(char *buf)
  109. {
  110.   static char *methods[] = { "GET", "HEAD", "POST", "PUT", "DELETE",
  111.                  "TRACE", "CONNECT", NULL };
  112.   char **pp;
  113.   int i;
  114.   
  115.   for (pp = methods; *pp != NULL; pp++) {
  116.     i = strlen(*pp);
  117.     if (strncmp(buf, *pp, i) == 0)
  118.       return (i);
  119.   }
  120.   return (0);
  121. }
  122.  
  123. int
  124. process_http_request(struct tuple4 *addr, u_char *buf, int len)
  125. {
  126.   char *p, *s, *e;
  127.   char *method, *uri, *vhost, *reqtime, *user, *referer, *agent;
  128.   int i, discard = 0;
  129.  
  130.   /* Process requests. */
  131.   for (s = buf; (e = bufbuf(s, len, "\r\n\r\n", 4)) != NULL; s = e + 4) {
  132.     i = (e + 4) - s;
  133.     discard += i; len -= i;
  134.     *e = '\0';
  135.     
  136.     method = uri = vhost = reqtime = user = referer = agent = NULL;
  137.     
  138.     /* Process header. */
  139.     for (p = strtok(s, "\r\n"); p != NULL; p = strtok(NULL, "\r\n")) {
  140.       if ((i = http_method_len(p)) != 0) {
  141.     method = p;
  142.     if (strlen(p) - i > 0) {
  143.       p[i] = '\0';
  144.       uri = p + i + 1;
  145.     }
  146.       }
  147.       else if (strncasecmp(p, "Authorization: Basic ", 21) == 0) {
  148.     p += 21;
  149.     i = base64_pton(p, p, strlen(p));
  150.     p[i] = '\0';
  151.     user = p;
  152.     if ((p = strchr(p, ':')) != NULL) *p = '\0';
  153.       }
  154.       else if (strncasecmp(p, "Host: ", 6) == 0)
  155.     vhost = p + 6;
  156.       else if (strncasecmp(p, "Referer: ", 9) == 0)
  157.     referer = p + 9;
  158.       else if (strncasecmp(p, "User-Agent: ", 12) == 0)
  159.     agent = p + 12;
  160.     }
  161.     reqtime = get_request_time();
  162.     
  163.     if (uri != NULL && strncasecmp(uri, "http://", 7) == 0)
  164.       vhost = "";
  165.     else if (vhost == NULL)
  166.       vhost = libnet_host_lookup(addr->daddr, Opt_dns);
  167.     
  168.     if (method != NULL && uri != NULL && vhost != NULL && reqtime != NULL) {
  169.       printf("%s - %s [%s] \"%s %s%s%s\" - - \"%s\" \"%s\"\n",
  170.          libnet_host_lookup(addr->saddr, Opt_dns),
  171.          (user ? user : "-"), reqtime, method,
  172.          (*vhost ? "http://" : vhost), vhost, uri,
  173.            (referer ? referer : "-"), (agent ? agent : "-"));
  174.     }
  175.   }
  176.   return (discard);
  177. }
  178.  
  179. void
  180. sniff_http_client(struct tcp_stream *ts, void **yoda)
  181. {
  182.   int i;
  183.  
  184.   /* Only handle HTTP client traffic. */
  185.   if (ts->addr.dest != 80 && ts->addr.dest != 3128 && ts->addr.dest != 8080)
  186.     return;
  187.  
  188.   switch (ts->nids_state) {
  189.   case NIDS_JUST_EST:
  190.     /* Collect data. */
  191.     ts->server.collect = 1;
  192.     
  193.   case NIDS_DATA:
  194.     if (ts->server.count_new != 0) {
  195.       i = process_http_request(&ts->addr, ts->server.data,
  196.                    ts->server.count - ts->server.offset);
  197.       nids_discard(ts, i);
  198.     }
  199.     break;
  200.     
  201.   default:
  202.     if (ts->server.count != 0) {
  203.       process_http_request(&ts->addr, ts->server.data,
  204.                ts->server.count - ts->server.offset);
  205.     }
  206.     break;
  207.   }
  208. }
  209.  
  210. void
  211. null_syslog(int type, int errnum, struct ip *iph, void *data)
  212. {
  213. }
  214.  
  215. int
  216. main(int argc, char *argv[])
  217. {
  218.   int c;
  219.   
  220.   while ((c = getopt(argc, argv, "i:nh?V")) != -1) {
  221.     switch (c) {
  222.     case 'i':
  223.       nids_params.device = optarg;
  224.       break;
  225.     case 'n':
  226.       Opt_dns = 0;
  227.       break;
  228.     case 'V':
  229.       fprintf(stderr, "Version: %s\n", VERSION);
  230.       usage();
  231.       break;
  232.     default:
  233.       usage();
  234.     }
  235.   }
  236.   argc -= optind;
  237.   argv += optind;
  238.  
  239.   if (argc != 0)
  240.     usage();
  241.  
  242.   nids_params.scan_num_hosts = 0;
  243.   nids_params.syslog = null_syslog;
  244.  
  245.   if (!nids_init()) {
  246.     fprintf (stderr, "%s\n", nids_errbuf);
  247.     exit(1);
  248.   }
  249.   nids_register_tcp(sniff_http_client);
  250.  
  251.   nids_run();
  252.  
  253.   /* NOTREACHED */
  254.   
  255.   exit(0);
  256. }
  257.  
  258. /* 5000. */
  259.